package com.caishi.lkx.distribution.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.caishi.lkx.distribution.mapper.CommissionRecordsMapper;
import com.caishi.lkx.distribution.mapper.DistributionWithdrawalSummaryMapper;
import com.caishi.lkx.distribution.mapper.DistributionWithdrawalsMapper;
import com.caishi.lkx.distribution.model.DistributionCommissionRecords;
import com.caishi.lkx.distribution.model.DistributionWithdrawalSummary;
import com.caishi.lkx.distribution.model.DistributionWithdrawals;
import com.caishi.lkx.distribution.service.ICommissionRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class CommissionRecordsServiceImpl extends ServiceImpl<CommissionRecordsMapper, DistributionCommissionRecords> implements ICommissionRecordsService {
    private final CommissionRecordsMapper commissionRecordsMapper;
    private final DistributionWithdrawalsMapper distributionWithdrawalsMapper;
    private final DistributionWithdrawalSummaryMapper distributionWithdrawalSummaryMapper;


    public CommissionRecordsServiceImpl(CommissionRecordsMapper commissionRecordsMapper, DistributionWithdrawalsMapper distributionWithdrawalsMapper, DistributionWithdrawalSummaryMapper distributionWithdrawalSummaryMapper) {
        this.commissionRecordsMapper = commissionRecordsMapper;

        this.distributionWithdrawalsMapper = distributionWithdrawalsMapper;
        this.distributionWithdrawalSummaryMapper = distributionWithdrawalSummaryMapper;
    }

//    @Override
//    public DistributorTransferInfo getDistributorTransferInfo() {
//        // 先获取提现记录的金额，再获取分佣记录的金额，两张表的记录不可删除
//        return null;
//    }

    /**
     * 小型系统进行全量统计，不删除数据与数据归档
     * 已验证其是否定时任务正常执行
     */
//    @Scheduled(cron = "0 * * * * ?")
    @Scheduled(cron = "0 0 12,0 * * ?")
    public void syncDistributorSummary() {

        List<DistributionCommissionRecords> distributionCommissionRecords = commissionRecordsMapper.selectCanTransfer();
        List<DistributionWithdrawals> distributionWithdrawals = distributionWithdrawalsMapper.selectIsTransferredAmount();
        // 将distributionCommissionRecords和distributionWithdrawals同userId的合并到DistributionWithdrawalSummary对象
        List<DistributionWithdrawalSummary> summaryList = new ArrayList<>();
        int max = Math.max(distributionCommissionRecords.size(), distributionWithdrawals.size());
        for (var i = 0; i < distributionCommissionRecords.size(); i++) {
            var record = distributionCommissionRecords.get(i);
            DistributionWithdrawalSummary summary = DistributionWithdrawalSummary.builder()
                    .userId(record.getUserId())
                    .build();
            // 理论上,如果所有人都提现过,同一排序规则下，同长度数组的数据用户id应该是相同的
            if (distributionCommissionRecords.size() == distributionWithdrawals.size()) {
                if (distributionWithdrawals.get(i).getUserId().equals(record.getUserId())) {
                    summary.setCanTransfer(record.getTotalAmount() - distributionWithdrawals.get(i).getTotalAmount());
                    summary.setIsTransferred(distributionWithdrawals.get(i).getTotalAmount());
                    summaryList.add(summary);
                    continue;
                }
            }
            Long isTransferred = distributionWithdrawals.stream().filter(w -> w.getUserId().equals(record.getUserId())).mapToLong(DistributionWithdrawals::getTotalAmount).sum();
            summary.setIsTransferred(isTransferred);
            summary.setCanTransfer(record.getTotalAmount() - isTransferred);

            summaryList.add(summary);
        }

        distributionWithdrawalSummaryMapper.batchSaveOrUpdate(summaryList);
    }
}
